import { Component, OnInit, OnDestroy } from '@angular/core';
import { TreeNode, DialogService } from 'primeng/primeng';
import { CustomService } from '../custom.service';
import { BizAddComponent } from './biz/biz-add.component'
@Component({
    templateUrl: './basic.component.html',
    styleUrls: ['./basic.component.scss']
})
export class BasicComponent implements OnInit {
    loading = true;
    customBizItemTree: TreeNode[];
    selectedNode: any;
    itemId: number = 0;
    bizId: number = 0;
    procDefId: number = 0;
    formDefId: number = 0;
    taskListGroupId: number = 0;
    names: string[] = [];
    constructor(
        private dialogService: DialogService,
        private customService: CustomService
    ) { }

    ngOnInit() {
        this.initCustomBizTree();
        //监听子组件中添加或删除事件，刷新树控件列表
        this.customService.routeMsgPush$.subscribe(data => this.initCustomBizTree());
    }
    /** 初始化自定义业务树 */
    initCustomBizTree() {
        this.customService.loadBizItemTree().subscribe(data => {
            this.loading = false;
            this.customBizItemTree = data;
            if (data.length == 0) return;
            let temp = data[0];
            temp.expanded = true;
            while (!temp.leaf) {
                temp = temp.children[0];
                temp.expanded = true;
            }
            this.selectedNode = temp;
            this.nodeSelect();
        });
    }
    nodeSelect() {
        if(!+this.selectedNode['id'])return;
        this.itemId = +this.selectedNode['id'];
        this.bizId = +this.selectedNode['data']['bizId'] || 0;
        this.procDefId = +this.selectedNode['data']['procDefId'] || 0;
        this.formDefId = +this.selectedNode['data']['formDefId'] || 0;
        this.taskListGroupId = +this.selectedNode['data']['taskListGroupId'] || 0;
        this.customService.loadFieldNames(this.itemId).subscribe(names => this.names = names);
    }
    /** 添加自定义业务 */
    addCustomBiz() {
        const ref = this.dialogService.open(BizAddComponent, {
            'header': '新增自定义业务',
            'width': '500px',
            contentStyle: { "height": "200px" }
        });
        ref.onClose.subscribe((data: any) => {
            if (data) {
                this.initCustomBizTree();
            }
        });
    }

    /** 为简化配置，隐藏部分功能 */
    handleChange(event) {
        if (event.index == 2) {
            // 流程配置简化，目前只做单业务、单流程
            $('ff-builder .biz-proc-tree-container').hide();
            $('ff-builder .biz-proc-detail-container').width('100%');
            $('ff-proc .ui-button-danger').hide();
        }
    }
}
